<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Users;
use app\web\controller\Express;
use think\Controller;
use app\common\controller\Alisms;
use think\Db;

/**
 * 手牌预付款控制器
 * @package app\index\controller
 */
class HandCardAdvance extends Auth
{
    public $noNeedLogin = [];

    /**
     * 预付款列表
     */
    public function advance_list()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if (empty($getData['page']) || empty($getData['limit'])) {
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_hand_card_advance')
                ->field('ha.*,c.name as viper_card_name,c.mobile,c.card_no,t.name as viper_type_name,u.real_name')
                ->alias('ha')
                ->leftJoin('qt_viper_card c','c.id = ha.viper_card_id')
                ->leftJoin('qt_viper_type t','t.id = c.viper_type_id')
                ->leftJoin('qt_users u','u.id = ha.user_id')
                ->where('ha.store_id', $userInfo['store_id'])
                ->order('ha.id desc')
                ->paginate($getData['limit']);
            $this->result('查询成功', $data, 200);
        } else {
            $this->result('无效的请求', '', 204);
        }
    }


    /**
     * 添加预付款
     */
    public function add_advance()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $validate = new \app\web\validate\HandCardAdvance();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            //判断密码是否正确
            $cardFind = Db::name('qt_viper_card')->where(['id'=>$getData['viper_card_id']])->find();
            if($getData['type'] == '1'){
                $passwd = pwd($getData['passwd'],$cardFind['salt']);
                if($passwd != $cardFind['passwd']){
                    $this->result('密码错误，请重新输入', '', 204);
                }
            }else{
                $sms = new Alisms();
                $checkPhone = $sms->check_smscode($getData['phone'], $getData['code'], 3);
                if ($checkPhone['code'] != 200) {
                    $this->result('验证码错误，请重新输入', '', 204);
                }
            }
            $getData['create_time'] = time();
            $getData['store_id'] = $this->userInfo['store_id'];
            $getData['user_id'] = $this->userInfo['id'];
            $insert = Db::name('qt_hand_card_advance')->insert($getData);
            if ($insert) {
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 删除预付款
     */
    public function delete_advance()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少参数', '', 204);
            }
            $del = Db::name('qt_hand_card_advance')->where('id', $getData['id'])->delete();
            if ($del) {
                $this->result('删除成功', '', 200);
            }
            $this->result('删除失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 保存预付款
     */
    public function save_advance()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $validate = new \app\web\validate\HandCardAdvance();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $del = Db::name('qt_hand_card_advance')->where('id', $getData['id'])->update($getData);
            if ($del !== false) {
                $this->result('保存成功', '', 200);
            }
            $this->result('保存失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 修改预付款
     */
    public function edit_advance()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少预付款信息', '', 204);
            }
            $data = Db::name('qt_hand_card_advance')
                ->field('a.*,c.mobile,c.name,c.amount,c.give_amount,c.score,t.name as viper_type_name')
                ->alias('a')
                ->leftJoin('qt_viper_card c','c.id = a.viper_card_id')
                ->leftJoin('qt_viper_type t','c.viper_type_id = t.id')
                ->where('a.id', $getData['id'])
                ->find();

            $this->result('查询成功', $data, 200);

        }
        $this->result('无效的请求', '', 204);
    }




}
